package com.xin.ruanjian.omsystem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.bean.GoodsBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by local123 on 2018/2/22.
 */

public class GoodsLeftAdapter extends RecyclerView.Adapter<GoodsLeftAdapter.ViewHolder> {
    private Context context;
//    private List<MenuBean> mData;
    private List<GoodsBean.MsgBean> mData;
    private onMenuSelectListener listener;
    private int selected = 0;

    public GoodsLeftAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<GoodsBean.MsgBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setListener(onMenuSelectListener listener) {
        this.listener = listener;
    }

    @Override
    public GoodsLeftAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shop_detail_left_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(GoodsLeftAdapter.ViewHolder holder, final int position) {
        if (position == selected) {
            holder.tvItemShopDetailMenu.setBackgroundResource(R.color.color_white);
//            holder.ivItemShopDetailMenu.setVisibility(View.VISIBLE);
        }else {
            holder.tvItemShopDetailMenu.setBackgroundResource(R.color.list_line);
//            holder.ivItemShopDetailMenu.setVisibility(View.GONE);
        }
        holder.tvItemShopDetailMenu.setText(mData.get(position).getMname());
        holder.tvItemShopDetailMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    setSelect(position);
                    listener.menuSelected(position, mData.get(position).getMid());
                }
            }
        });
    }

    public void setSelect(int position) {
        notifyItemChanged(selected);
        selected = position;
        notifyItemChanged(position);
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_item_shop_detail_menu)
        TextView tvItemShopDetailMenu;
        @BindView(R.id.iv_item_shop_detail_menu)
        ImageView ivItemShopDetailMenu;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface onMenuSelectListener {
        void menuSelected(int position, int menuId);
    }
}
